<?php
require_once '../DTO/TinRaoVat.php';
require_once 'DBConnection.php';
require_once 'ChiTietTinhRaoVatDAO.php';

class TinRaoVatDAO extends DBConnection{
	
	public function __construct(){
		parent::__construct();
	}
	
	//Lấy danh sách tất cả các tin rao vặt
	public function getTinRaoVat(){
		$sql = "select * from tinraovat";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting tin rao vat");
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy danh sách tất cả tin rao vặt sắp xếp theo thời gian giảm dần
	public function getTinRaoVatSortByTime($index, $number){
		$sql = "select * from tinraovat order by NgayRao DESC limit $index,$number";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt sắp xếp theo giá giảm dần
	public function getTinRaoVatSortByGiaDESC($index, $number){
		$sql = "select * from tinraovat order by GiaCa DESC limit $index,$number";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat by gia DESC");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt sắp xếp theo giá tăng dần
	public function getTinRaoVatSortByGiaASC($index, $number){
		$sql = "select * from tinraovat order by GiaCa ASC limit $index,$number";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat by gia ASC");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt theo mã người dùng
	public function getTinRaoVatByMaND($maND, $index, $number){
		$sql = "select * from tinraovat where MaND='$maND' limit $index,$number";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm số lượng trv theo mã người dùng
	public function getSoLuongByMaND($maND){
		$sql = "select count(*) as SoLuong from tinraovat where MaND='$maND'";
		return $this->extractSoLuong($sql);
	}
	
	//Lấy tất cả tin rao vặt theo mã sản phẩm
	public function getTinRaoVatByMaSP($maSP, $index, $number){
		$sql = "select * from tinraovat where MaSP='$maSP' limit $index,$number";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm số lượng trv theo mã sản phẩm
	public function getSoLuongTRVByMaSP($maSP){
		$sql = "select count(*) as SoLuong from tinraovat where MaSP='$maSP'";
		return $this->extractSoLuong($sql);
	}
	
	//Lấy tất cả tin rao vặt theo mã vùng
	public function getTinRaoVatByMaVung($maVung, $index, $number){
		$sql = "SELECT * FROM tinraovat WHERE MaTRV in (select MaTRV from chitiettinhraovat 
				where MaTinh in (select MaTinh from tinh where MaVung='$maVung')) limit $index,$number";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm số lượng trv theo mã vùng
	public function getSoLuongTRVByMaVung($maVung){
		$sql = "select count(*) as SoLuong from tinraovat WHERE MaTRV in 
		(select MaTRV from chitiettinhraovat where MaTinh in 
		(select MaTinh from tinh where MaVung='$maVung'))";
		return $this->extractSoLuong($sql);
	}
	
	//Lấy tất cả tin rao vặt theo mã tỉnh
	public function getTinRaoVatByMaTinh($maTinh, $index, $number){
		$sql = "SELECT * FROM tinraovat WHERE MaTRV in (select MaTRV from chitiettinhraovat 
				where MaTinh='$maTinh') limit $index,$number";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm số lượng trv theo mã tinh
	public function getSoLuongTRVByMaTinh($maTinh){
		$sql = "select count(*) as SoLuong from tinraovat where MaTRV in 
		(select MaTRV from chitiettinhraovat where MaTinh='$maTinh')";
		return $this->extractSoLuong($sql);
	}
	
	//Lấy tin rao vặt theo mã tin
	public function getTinRaoVatByMaTRV($maTRV){
		$sql = "SELECT * FROM tinraovat WHERE MaTRV='$maTRV'";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting tin rao vat");
		}
		
		if(mysql_num_rows($res)){			
			$temp = mysql_fetch_assoc($res);			
			$obj = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
			return $obj;
		}else{
			return null;
		}
	}
	
	//Lấy danh sách tin rao vặt theo phân trang
	public function getTinRaoVatPaging($index, $number){
		$sql = "SELECT * FROM tinraovat limit $index,$number limit $index,$number";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting tin rao vat paging");
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm tin rao vặt theo mã danh mục sản phẩm
	public function getTinRaoVatByMaDMSP($maDMSP, $index, $number){
		$sql = "select * from tinraovat where MaSP in (select MaSP from sanpham where MaDMSP=$maDMSP) limit $index,$number";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting tin rao vat  by madmsp");
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], 
						$temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm số lượng trv theo mã danh mục sản phẩm
	public function getSoLuongTRVByMaDMSP($maDMSP){
		$sql = "select count(*) as SoLuong from tinraovat where MaSP in 
		(select MaSP from sanpham where MaDMSP=$maDMSP)";
		return $this->extractSoLuong($sql);
	}
	
	public function extractTRV($sql){
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting tin rao vat");
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'],
						$temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm tin rao vặt theo tiêu đề, tất cả các sản phẩm, tất cả các vùng
	public function getTRVByTieuDe($tieuDe, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' 
		and GiaCa between $giaTu and $giaDen limit $index,$number";
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, tất cả sản phẩm, tất cả các vùng
	public function getSoLuongTRVByTieuDe($tieuDe, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' 
		and GiaCa between $giaTu and $giaDen";
		return $this->extractSoLuong($sql);
	}
	
	//Tìm tin rao vặt theo tiêu đề, mã danh mục sản phẩm
	public function getTRVByTieuDeDMSP($tieuDe, $maDMSP, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP in (select MaSP from sanpham where MaDMSP='$maDMSP') and GiaCa between $giaTu and $giaDen
		 limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã danh mục sản phẩm
	public function getSoLuongTRVByTieuDeDMSP($tieuDe, $maDMSP, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP in (select MaSP from sanpham where MaDMSP='$maDMSP') and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}	
	
	//Tìm tin rao vặt theo tiêu đề, mã sản phẩm
	public function getTRVByTieuDeMSP($tieuDe, $maSP, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and
		MaSP='$maSP' and GiaCa between $giaTu and $giaDen limit $index,$number";
			
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã sản phẩm
	public function getSoLuongTRVByTieuDeMSP($tieuDe, $maSP, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and
		MaSP='$maSP' and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}
		
	//Tìm tin rao vặt theo tiêu đề, mã vùng
	public function getTRVByTieuDeMaVung($tieuDe, $maVung, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh in (select MaTinh from tinh where 
		MaVung='$maVung')) and GiaCa between $giaTu and $giaDen limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã vùng
	public function getSoLuongTRVByTieuDeMaVung($tieuDe, $maVung, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh in (select MaTinh from tinh where 
		MaVung='$maVung')) and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}
	
	//Tìm tin rao vặt theo tiêu đề, mã tỉnh
	public function getTRVByTieuDeMaTinh($tieuDe, $maTinh, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and MaTRV in 
		(select MaTRV from chitiettinhraovat where MaTinh='$maTinh') and GiaCa between $giaTu and $giaDen
		 limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã tỉnh
	public function getSoLuongTRVByTieuDeMaTinh($tieuDe, $maTinh, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and MaTRV in 
		(select MaTRV from chitiettinhraovat where MaTinh='$maTinh') and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}
	
	//Tìm tin rao vặt theo tiêu đề, mã danh mục sản phẩm, mã vùng
	public function getTRVByTieuDeMaDMSPMaVung($tieuDe, $maDMSP, $maVung, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP in (select MaSP from sanpham where MaDMSP='$maDMSP') and
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh in (select MaTinh from tinh where 
		MaVung='$maVung')) and GiaCa between $giaTu and $giaDen limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã danh mục sản phẩm, mã vùng
	public function getSoLuongTRVByTieuDeMaDMSPMaVung($tieuDe, $maDMSP, $maVung, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP in (select MaSP from sanpham where MaDMSP='$maDMSP') and
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh in (select MaTinh from tinh where 
		MaVung='$maVung')) and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}
	
	//Tìm tin rao vặt theo tiêu đề, mã danh mục sản phẩm, mã tỉnh
	public function getTRVByTieuDeMaDMSPMaTinh($tieuDe, $maDMSP, $maTinh, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP in (select MaSP from sanpham where MaDMSP='$maDMSP') and 
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh='$maTinh') 
		and GiaCa between $giaTu and $giaDen limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã danh mục sản phẩm, mã tỉnh
	public function getSoLuongTRVByTieuDeMaDMSPMaTinh($tieuDe, $maDMSP, $maTinh, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP in (select MaSP from sanpham where MaDMSP='$maDMSP') and 
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh='$maTinh') 
		and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}
	
	//Tìm tin rao vặt theo tiêu đề, mã sản phẩm, mã vùng
	public function getTRVByTieuDeMaSPMaVung($tieuDe, $maSP, $maVung, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP='$maSP' and MaTRV in (select MaTRV from chitiettinhraovat where MaTinh in 
		(select MaTinh from tinh where MaVung='$maVung')) 
		and GiaCa between $giaTu and $giaDen limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã sản phẩm, mã vùng
	public function getSoLuongTRVByTieuDeMaSPMaVung($tieuDe, $maSP, $maVung, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and 
		MaSP='$maSP' and MaTRV in (select MaTRV from chitiettinhraovat where MaTinh in 
		(select MaTinh from tinh where MaVung='$maVung')) 
		and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}
	
	//Tìm tin rao vặt theo tiêu đề, mã sản phẩm, mã tỉnh
	public function getTRVByTieuDeMaSPMaTinh($tieuDe, $maSP, $maTinh, $giaTu, $giaDen, $index, $number){
		$sql = "select * from tinraovat where TieuDe like '%$tieuDe%' and MaSP='$maSP' and 
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh='$maTinh') 
		and GiaCa between $giaTu and $giaDen limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm số lượng trv theo tiêu đề, mã sản phẩm, mã tỉnh
	public function getSoLuongTRVByTieuDeMaSPMaTinh($tieuDe, $maSP, $maTinh, $giaTu, $giaDen){
		$sql = "select count(*) as SoLuong from tinraovat where TieuDe like '%$tieuDe%' and MaSP='$maSP' and 
		MaTRV in (select MaTRV from chitiettinhraovat where MaTinh='$maTinh') 
		and GiaCa between $giaTu and $giaDen";
		
		return $this->extractSoLuong($sql);
	}
	
	//Tìm số lượng tin rao vặt theo mã loai người dùng
	public function getSoLuongTRVByMaLND($maLND){
		$sql = "select count(*) as SoLuong from tinraovat where MaND in 
		(select MaND from nguoidung where MaLND='$maLND')";
		
		return $this->extractSoLuong($sql);
	}
	
	//Tìm tin rao vặt theo mã loại người dùng
	public function getTRVByMaLND($maLND, $index, $number){
		$sql = "select * from tinraovat where MaND in 
		(select MaND from nguoidung where MaLND='$maLND')
		limit $index,$number";
		
		return $this->extractTRV($sql);
	}
	
	//Tìm giá lớn nhất
	public function getMaxGiaCa(){
		$sql = "select MAX(GiaCa) as MaxGia from tinraovat";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting MAX(GiaCa)");
		}
		
		if(mysql_num_rows($res)){
			$obj = mysql_fetch_assoc($res);
			return $obj['MaxGia'];
		}else{
			return null;
		}		
	}
	
	//Tìm số lượng tất cả tin rao vặt
	public function getSoLuongTRV(){
		$sql = "select count(*) as SoLuong from tinraovat";
		
		return $this->extractSoLuong($sql);
	}
	
	public function extractSoLuong($sql){
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting so luong tin rao vat");
		}
		
		if(mysql_num_rows($res)){
			$obj = mysql_fetch_assoc($res);
			return $obj['SoLuong'];
		}else{
			return null;
		}
	}
	
	//Insert into tinraovat
	public function insert(TinRaoVat $tinraovat, $maTinh){
		$maLoaiTRV = $tinraovat->getMaLoaiTRV();
		$tieuDe = $tinraovat->getTieuDe();
		$giaCa = $tinraovat->getGiaCa();
		$noiDung = $tinraovat->getNoiDung();
		$hinhAnh = $tinraovat->getHinhAnh();
		$ngayRao = $tinraovat->getNgayRao();
		$soLuongTruyCap = $tinraovat->getSoLuongTruyCap();
		$maND = $tinraovat->getMaND();
		$maSP = $tinraovat->getMaSP();
		
		$sql = "insert into tinraovat (MaLoaiTRV, TieuDe, GiaCa, NoiDung, HinhAnh, 
		NgayRao, SoLuongTruyCap, MaND, MaSP) values ('$maLoaiTRV', '$tieuDe', '$giaCa', '$noiDung', '$hinhAnh',
		'$ngayRao', '$soLuongTruyCap', '$maND', '$maSP')";
		
		$res = mysql_query($sql);
		
		if(!$res){
			die("Faile insert into tinraovat");
		}
		
		$maTRV = $this->getMaxMaTRV();
		$chiTietTinhDAO = new ChiTietTinhRaoVatDAO();
		foreach ($maTinh as $obj){
			$chiTietTinh = new ChiTietTinhRaoVat($maTRV, $obj);
			$chiTietTinhDAO->insert($chiTietTinh);
		}
	}
	
	//Delete from tinraovat
	public function delete($maTRV){
		$sql = "delete from tinraovat where MaTRV='$maTRV'";
	
		$res = mysql_query($sql);
	
		if(!$res){
			die("Failed delete from tinraovat");
		}
	}
	
	public function getMaxMaTRV(){
		$sql = "select max(MaTRV) as maxMaTRV from tinraovat";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!$res){
			die("Failed getting max MaTRV");
		}
	
		if(mysql_num_rows($res)){
			$temp = mysql_fetch_assoc($res);
			return $temp['maxMaTRV'];
		}else{
			return null;
		}
	}

}



















